<?php

namespace app\api\controller;

use app\BaseController;
use frame\lib\Captcha;
use frame\lib\Db;

class Fake extends Base
{
  public function use()
  {
    $fake = @$_GET['fake'];
    if (empty($fake)) error('卡密不能为空', 201);
    $Fake = Db::name('fake')->where(['fake' => $fake])->find();
    if (!$Fake) error('卡密不存在');
    if ($Fake['status'] == 1) error('卡密已被使用');
    $userData = Db::name('user')->where(['id' => $this->uid])->find();
    if($Fake['type'] == 3){
      $score = $userData['score'] + $Fake['score'];
      Db::name('user')->where(['id' => $this->uid])->update(['score' => $score]);
    }else if($Fake['type'] == 2){
      $vip_time = getTime(3 , $userData['vip_time']) > time() ? getTime('3' ,  $userData['vip_time']) + $Fake['vip_time'] : time() + $Fake['vip_time'];
      Db::name('user')->where(['id' => $this->uid])->update(['vip_time' => getTime(2,$vip_time)]);
    }
    $res = Db::name('fake')->where(['fake' => $fake])->update(['use_user_id' => $this->uid, 'status' => 1 , 'use_time' => getTime()]);
    if ($res) success();
    error();
  }
}
